//
//  StatusCell.swift
//  Weibo
//
//  Created by 李振兴 on 15/10/20.
//  Copyright © 2015年 李振兴. All rights reserved.
//

import UIKit
/// 微博 Cell 中控件的间距数值
let StatusCellMargin: CGFloat = 12
/// 微博头像的宽度
let StatusCellIconWidth: CGFloat = 35
///微博Cell
class StatusCell: UITableViewCell {
    
    var viewModel:StatusViewModel?{
        didSet{
            topView.viewModel = viewModel
            
            contentLabel.text = viewModel?.status.text
            // 设置配图视图 － 设置视图模型之后，配图视图有能力计算大小
            pictureView.viewModel = viewModel
            
            pictureView.snp_updateConstraints { (make) -> Void in
                make.height.equalTo(pictureView.bounds.height)
                // 直接设置宽度数值
                make.width.equalTo(pictureView.bounds.width)
                //防止图片为空时出现两个margin
//                let offset = viewModel?.thumbnailUrls?.count > 0 ? StatusCellMargin:0
//                make.top.equalTo(contentLabel.snp_bottom).offset(offset)
            }

        }
    }
    /// 根据指定的视图模型计算行高
    ///
    /// - parameter vm: 视图模型
    ///
    /// - returns: 返回视图模型对应的行高
    func rowHeight(vm: StatusViewModel) -> CGFloat {
        // 1. 记录视图模型 -> 会调用上面的 didSet 设置内容以及更新`约束`
        viewModel = vm
        
        // 2. 强制更新所有约束 -> 所有控件的frame都会被计算正确
        contentView.layoutIfNeeded()
        
        // 3. 返回底部视图的最大高度
        return CGRectGetMaxY(bottomView.frame)
    }

    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        setupUI()
        
        //选中样式
        selectionStyle = .None
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
     lazy var topView:StatusCellTopView = StatusCellTopView()
    /// 微博正文标签
     lazy var contentLabel: UILabel = UILabel(title: "微博正文",
        fontSize: 15,
        color: UIColor.darkGrayColor(),
        screenInset: StatusCellMargin)
    /// 配图视图
     lazy var pictureView: StatusPictureView = StatusPictureView()
     lazy var bottomView:StatusCellButtomView = StatusCellButtomView()
    
}
extension StatusCell{
     func setupUI(){
      contentView.addSubview(topView)
      contentView.addSubview(contentLabel)
      contentView.addSubview(pictureView)
      contentView.addSubview(bottomView)
        ///设置约束
        topView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * StatusCellMargin+StatusCellIconWidth )
        }
        // 2> 内容标签
        contentLabel.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
            
            // 指定向下的约束
            //make.bottom.equalTo(contentView.snp_bottom).offset(-12)
        }
        // 3> 配图视图
//        pictureView.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
//            make.left.equalTo(contentLabel.snp_left)
//            make.width.equalTo(300)
//            make.height.equalTo(90)
//        }
        // 4> 底部视图
        bottomView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            
            // 指定向下的约束
            //make.bottom.equalTo(contentView.snp_bottom)
        }

        
    }
}
